package com.zhoug.player.exoplayer.v2;

import android.net.Uri;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media3.common.Format;
import androidx.media3.common.util.TimestampAdjuster;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.hls.HlsExtractorFactory;
import androidx.media3.exoplayer.hls.HlsMediaChunkExtractor;
import androidx.media3.extractor.ExtractorInput;
import androidx.media3.extractor.text.SubtitleParser;

/**
 * @author: zhoug
 * @since: 2025-06-27
 */
@UnstableApi public class ZDefaultHlsExtractorFactory  implements HlsExtractorFactory {
    private final HlsExtractorFactory defaultFactory;
    private boolean audioFormatSupported = true;

    public ZDefaultHlsExtractorFactory() {
        this.defaultFactory=HlsExtractorFactory.DEFAULT;
    }

    @NonNull
    @Override
    public HlsMediaChunkExtractor createExtractor(@NonNull Uri uri,
                                                  @NonNull Format format,
                                                  @Nullable List<Format> muxedCaptionFormats,
                                                  @NonNull TimestampAdjuster timestampAdjuster,
                                                  @NonNull Map<String, List<String>> responseHeaders,
                                                  @NonNull ExtractorInput sniffingExtractorInput,
                                                  @NonNull PlayerId playerId) throws IOException {
        // 检测到音频格式且之前已失败，跳过音频


        return defaultFactory.createExtractor(uri,format,muxedCaptionFormats,timestampAdjuster,responseHeaders,sniffingExtractorInput,playerId);
    }

    @NonNull
    @Override
    public HlsExtractorFactory setSubtitleParserFactory(@NonNull SubtitleParser.Factory subtitleParserFactory) {
        return defaultFactory.setSubtitleParserFactory(subtitleParserFactory);
    }

    @NonNull
    @Override
    public HlsExtractorFactory experimentalParseSubtitlesDuringExtraction(boolean parseSubtitlesDuringExtraction) {
        return defaultFactory.experimentalParseSubtitlesDuringExtraction(parseSubtitlesDuringExtraction);

    }

    @NonNull
    @Override
    public Format getOutputTextFormat(@NonNull Format sourceFormat) {
        return defaultFactory.getOutputTextFormat(sourceFormat);

    }

    private boolean isAudioFormat(@NonNull Format format) {
        return format.sampleMimeType != null && format.sampleMimeType.startsWith("audio/");
    }

}
